//
//  YBLoginRegisterInputView.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/1.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBLoginRegisterInputView: UIView {
    
    enum InputType: Int {
        case phone = 1000
        case pwd = 1001
        case code = 1002
        
        var style: (icon: String, title: String, des: String) {
            switch self {
            case .phone: return ("icon_login_phone", "手机号", "请输入手机号")
            case .pwd: return ("icon_login_pwd", "密码", "请输入6-10数字、字母组成的密码")
            case .code: return ("icon_login_code", "验证码", "请输入验证码")
            }
        }
    }
    
    /// 输入框文本
    var text: String {
        get { return inputTextField.text ?? "" }
        set { inputTextField.text = newValue }
    }
    
    var onTextField: ((String) -> Void)?
    
    private let inputType: InputType
    
    init(inputType: InputType) {
        self.inputType = inputType
        super.init(frame: .zero)
        
        self.addSubviews([titleBtn, inputTextField])
        inputTextField.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(45)
        }
        
        titleBtn.snp.makeConstraints { make in
            make.bottom.equalTo(inputTextField.snp.top).offset(-8)
            make.left.equalTo(9)
            make.top.equalToSuperview().priority(.high)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var titleBtn: BKLayoutButton = {
        let btn = self.bk_addLayoutButton(style: .leftImageRightTitle, space: 7.5, bgColor: .clear, imageSize: CGSizeMake(13, 16), image: UIImage(named: inputType.style.icon), titleFont: .systemFont(ofSize: 14, weight: .medium), title: inputType.style.title, titleColor: .lightBlack51DarkLight230)
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    private lazy var inputTextField: UITextField = {
        let tf = UITextField()
        tf.borderStyle = .none
        tf.backgroundColor = .lightGray248Dark33
        tf.bk_addPlaceholder(inputType.style.des, color: ColorDef.gray201)
        tf.bk_addBlankLeftView(width: 25)
        tf.tag = inputType.rawValue
        tf.textColor = .lightBlack51DarkLight230
        tf.font = .systemFont(ofSize: 15)
        tf.clearButtonMode = .whileEditing
        tf.isSecureTextEntry = inputType == .pwd
        tf.keyboardType = inputType == .pwd ? .default : .numberPad
        tf.bk_addStyleWith(cornerRadius: 8, corners: .allCorners)
        tf.bk_addTarget { [unowned self] tf in
            self.onTextField?(tf.text ?? "")
        }
        return tf
    }()
    
}
